using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Oops.Xna.Framework.Physics;

namespace Oops.Xna.Framework.Testing.App
{
	public class Example1Screen : ExampleScreen
	{
		// Content.
		private Texture2D planeTexture;
		private Matrix planeOrientation = Matrix.CreateRotationY(MathHelper.ToRadians(-10.0f));
		
		// Drawing helpers.
		private Quadrilateral quad;
		
		public Example1Screen(Game game) : base(game, "Example1") 
		{
			// Drawing helper for the ground.
			this.quad = new Quadrilateral(Vector3.Zero, Vector3.Up, Vector3.Backward, 260.0f, 260.0f, 10, 10);
		}

		#region ExampleScreen Members

		protected override void DrawWorld(Effect effect, EffectParameter world, EffectParameter texture)
		{
			// Draw the ground.
			world.SetValue(this.planeOrientation);
			texture.SetValue(this.planeTexture);
			effect.CommitChanges();
			this.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, this.quad.Vertices, 0, 4, this.quad.Indices, 0, 2);
		}

		protected override void Reset()
		{
			// Start with 50 boxes and 150 spheres.
			this.Reset(100, 150);

			// Create the ground plane.  
			CollisionPlane plane = new CollisionPlane();
			plane.Normal = Vector3.Transform(Vector3.UnitY, this.planeOrientation);
			
			// Create a collision group and add the plane.
			CollisionGroup collisionGroup = new CollisionGroup();
			collisionGroup.CollisionShapes.Add(plane);

			// Add the plane's collision group to the collision system.
			this.physics.CollisionSystem.CollisionGroups.Add(collisionGroup);
		}

		#endregion

		#region DrawableGameComponent Members

		protected override void LoadContent()
		{
			// Load all the used texture resources.
			this.planeTexture = this.Game.GetContent().Load<Texture2D>(@".\Content\Textures\plane");
			
			// Call inherited method.
			base.LoadContent();
		}

		#endregion
	}
}
